package ro.gateway.aida.obj.db;

/**
 * <p>Title: Romanian AIDA</p>
 * <p>Description: :D application</p>
 * <p>Copyright: Copyright (comparator) 2003</p>
 * <p>Company: Romania Development Gateway </p>
 * @author Mihai Popoaei, mihai_popoaei@yahoo.com, smike@intellisource.ro
 * @version 1.0-* @version $Id: AIDABasicObjectDB.java,v 1.1 2004/10/24 23:37:11 mihaipostelnicu Exp $
 *
 * @deprecated
 */
public class AIDABasicObjectDB
{
      public static long getLastId( ro.gateway.aida.srv.ICFService cfService, String poolName )
            throws java.sql.SQLException
      {
            long result = 0;
            java.sql.Connection con = cfService.getConnection( poolName );

            try
            {
                  java.sql.PreparedStatement ps = con.prepareStatement( GET_MAX_ID );
                  java.sql.ResultSet rs = ps.executeQuery();
                  if ( rs.next() )
                  {
                        result = rs.getLong( 1 );
                  }
                  rs.close();
                  ps.close();
            }
            catch ( java.sql.SQLException sqlEx )
            {
                  throw sqlEx;
            }
            finally
            {
                  con.close();
            }

            return result;
      }

//      public static void insert(
//            ro.gateway.aida.srv.ICFService cfService,
//            String poolName,
//            ro.gateway.aida.srv.IIDGenerator idGenerators,
//            ro.gateway.aida.obj.AIDABasicObject item )
//            throws java.sql.SQLException
//      {
//
//            java.sql.Connection con = cfService.getConnection( poolName );
//
//            try
//            {
//                  insert( con, idGenerators, item );
//            }
//            catch ( java.sql.SQLException sqlEx )
//            {
//                  throw sqlEx;
//            }
//            finally
//            {
//                  con.close();
//            }
//      }

//      static void insert(
//            java.sql.Connection con,
//            ro.gateway.aida.srv.IIDGenerator idGenerators,
//            ro.gateway.aida.obj.AIDABasicObject item )
//            throws java.sql.SQLException
//      {
//
//            if ( item.getId() == -1 )
//            {
//                  item.setId( idGenerators.nextVal( ro.gateway.aida.servlet.Constants.IDGEN_OBJECTS ) );
//            }
//
//            java.sql.PreparedStatement ps = con.prepareStatement( INSERT );
//
//            ps.setLong( 1, item.getId() );
//            ps.setByte( 2, item.getObjectType() );
//            long origin = item.getOrigin();
//            if ( origin == -1 )
//            {
//                  ps.setNull( 3, java.sql.Types.INTEGER );
//            }
//            else
//            {
//                  ps.setLong( 3, item.getOrigin() );
//            }
//            ps.setString( 4, item.getOriginDbKey() );
//            ps.setDate( 5, new java.sql.Date( item.getDate_modif() ) );
//            ps.setDate( 6, new java.sql.Date( item.getHarvest_date() ) );
//            if ( item.getUser_id() == -1 )
//            {
//                  ps.setNull( 7, java.sql.Types.INTEGER );
//            }
//            else
//            {
//                  ps.setLong( 7, item.getUser_id() );
//            }
//            ps.executeUpdate();
//
//            ps.close();
//      }


      public static void delete(
            ro.gateway.aida.srv.ICFService cfService,
            String poolName,
            long id )
            throws java.sql.SQLException
      {
            java.sql.Connection con = cfService.getConnection( poolName );

            try
            {
                  java.sql.PreparedStatement ps = con.prepareStatement( DELETE );
                  ps.setLong( 1, id );
                  ps.executeUpdate();
                  ps.close();
            }
            catch ( java.sql.SQLException sqlEx )
            {
                  throw sqlEx;
            }
            finally
            {
                  con.close();
            }
      }

      public static void update(
            ro.gateway.aida.srv.ICFService cfService,
            String poolName,
            ro.gateway.aida.obj.AIDABasicObject item )
            throws java.sql.SQLException
      {

            if ( item.getId() == -1 )
            {
                  System.err.println( "Incercare de update object fara id" );
                  return;
            }
            java.sql.Connection con = cfService.getConnection( poolName );

            try
            {
                  java.sql.PreparedStatement ps = con.prepareStatement( UPDATE );

                  ps.setByte( 1, item.getObjectType() );
                  long origin_db = item.getOrigin();
                  if ( origin_db == -1 )
                  {
                        ps.setNull( 2, java.sql.Types.INTEGER );
                  }
                  else
                  {
                        ps.setLong( 2, item.getOrigin() );
                  }
                  ps.setString( 3, item.getOriginDbKey() );
                  ps.setDate( 4, new java.sql.Date( item.getDate_modif() ) );
                  ps.setDate( 5, new java.sql.Date( item.getHarvest_date() ) );
                  if ( item.getUser_id() == -1 )
                  {
                        ps.setNull( 6, java.sql.Types.INTEGER );
                  }
                  else
                  {
                        ps.setLong( 6, item.getUser_id() );
                  }
                  ps.setLong( 7, item.getId() );
                  ps.executeUpdate();

                  ps.close();
            }
            catch ( java.sql.SQLException sqlEx )
            {
                  throw sqlEx;
            }
            finally
            {
                  con.close();
            }

            return;
      }

      public static ro.gateway.aida.obj.AIDABasicObject getById(
            ro.gateway.aida.srv.ICFService cfService,
            String poolName,
            long id )
            throws java.sql.SQLException
      {

            java.sql.Connection con = cfService.getConnection( poolName );
            ro.gateway.aida.obj.AIDABasicObject result = null;

            try
            {
                  java.sql.PreparedStatement ps = con.prepareStatement( GET_BY_ID );
                  ps.setLong( 1, id );
                  java.sql.ResultSet rs = ps.executeQuery();
                  if ( rs.next() )
                  {
                        result = new ro.gateway.aida.obj.AIDABasicObject();
                        result.setId( id );
                        result.setObjectType( rs.getByte( 1 ) );
                        result.setOrigin( rs.getLong( 2 ) );
                        if ( rs.wasNull() )
                        {
                              result.setOrigin( -1 );
                        }
                        result.setOriginDbKey( rs.getString( 3 ) );
                        result.setDate_modif( rs.getTimestamp( 4 ).getTime() );
                        result.setHarvest_date( rs.getTimestamp( 5 ).getTime() );
                        result.setUser_id( rs.getLong( 6 ) );
                        if ( rs.wasNull() )
                        {
                              result.setUser_id( -1 );
                        }
                  }
                  rs.close();
                  ps.close();
            }
            catch ( java.sql.SQLException sqlEx )
            {
                  throw sqlEx;
            }
            finally
            {
                  con.close();
            }

            return result;
      }

      /**
       * ???
       * @deprecated
       * @param cfService
       * @param poolName
       * @param result
       * @throws java.sql.SQLException
       */
      private static void getById(
            ro.gateway.aida.srv.ICFService cfService,
            String poolName,
            ro.gateway.aida.obj.AIDABasicObject result )
            throws java.sql.SQLException
      {

            if ( result == null )
            {
                  System.err.println( "getById (aida object) -> para null" );
                  return;
            }

            java.sql.Connection con = cfService.getConnection( poolName );

            try
            {
                  java.sql.PreparedStatement ps = con.prepareStatement( GET_BY_ID );
                  ps.setLong( 1, result.getId() );
                  java.sql.ResultSet rs = ps.executeQuery();
                  if ( rs.next() )
                  {
                        result = new ro.gateway.aida.obj.AIDABasicObject();
                        result.setObjectType( rs.getByte( 1 ) );
                        result.setOrigin( rs.getLong( 2 ) );
                        result.setOriginDbKey( rs.getString( 3 ) );
                        result.setDate_modif( rs.getTimestamp( 4 ).getTime() );
                        result.setHarvest_date( rs.getTimestamp( 5 ).getTime() );
                        result.setUser_id( rs.getLong( 6 ) );
                  }
                  rs.close();
                  ps.close();
            }
            catch ( java.sql.SQLException sqlEx )
            {
                  throw sqlEx;
            }
            finally
            {
                  con.close();
            }
      }

      private static final String GET_MAX_ID =
            "SELECT max(id) FROM objects";

//      private static final String INSERT =
//            "INSERT INTO objects (id, obj_type, origin, " +
//            "origin_dbk, date_modif, harvest_date, user_id) " +
//            "VALUES (?,?,?,?,?,?,?)";

      private static final String UPDATE =
            "UPDATE objects SET obj_type = ?, origin = ?, " +
            "origin_dbk = ?, date_modif = ?, harvest_date = ?, user_id = ? " +
            "WHERE id = ?";

      private static final String GET_BY_ID =
            "SELECT obj_type, origin, origin_dbk, date_modif, harvest_date, user_id " +
            "FROM objects " +
            "WHERE id = ?";

      private static final String DELETE =
            "DELETE FROM objects WHERE id=?";
}
